package com.canton.controller;

import com.canton.service.CountService;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


@RestController
@RequestMapping(value = "/count", produces="application/json;charset=utf-8")
@CrossOrigin//允许跨域访问
public class CountController {
    @Autowired
    private CountService countService;

    @GetMapping("/getCount")
    @SneakyThrows
    @ApiOperation(value="获取三元组 实体 关系 书籍数量")
    @ResponseBody
    public Map<String, Integer> getCount() throws Exception {
        CompletableFuture<Integer> countArea = countService.countArea();
        CompletableFuture<Integer> countClass = countService.countClass();
        CompletableFuture<Integer> countClip = countService.countClip();
        CompletableFuture<Integer> countClimate = countService.countClimate();
        CompletableFuture<Integer> countDrug = countService.countDrug();
        CompletableFuture<Integer> countCultivation = countService.countCultivation();
        CompletableFuture<Integer> countDisease = countService.countDisease();
        CompletableFuture<Integer> countGrape = countService.countGrape();
        CompletableFuture<Integer> countImage = countService.countImage();
        CompletableFuture<Integer> countMethod = countService.countMethod();
        CompletableFuture<Integer> countPest = countService.countPest();
        CompletableFuture<Integer> countSymptom = countService.countSymptom();
        // 等待所有任务都执行完
        CompletableFuture.allOf(countArea,countClass,countClip,countClimate,countDrug,
                countDisease,countCultivation,countGrape,countImage,countMethod,countPest,countSymptom).join();
        // 获取每个任务的返回结果
        Map<String, Integer> map = new HashMap<>();
        map.put("countArea",countArea.get());
        map.put("countClass",countClass.get());
        map.put("countClimate",countClimate.get());
        map.put("countDrug",countDrug.get());
        map.put("countSymptom",countSymptom.get());
        map.put("countDisease",countDisease.get());
        map.put("countClip",countClip.get());
        map.put("countCultivation",countCultivation.get());
        map.put("countGrape",countGrape.get());
        map.put("countImage",countImage.get());
        map.put("countMethod",countMethod.get());
        map.put("countPest",countPest.get());
        map.put("countAll",countPest.get()+countMethod.get()+countImage.get()+countGrape.get()+countCultivation.get()+countClip.get()+countDisease.get()+
                countSymptom.get()+countDrug.get()+countClimate.get()+countClass.get()+countArea.get());

//        System.out.println(countClimate);
        return map;
    }
}
